package bill.study.annotation.effectivejava.item35;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RunTests {
	public static void main(String[] args) throws Exception {
		int passed = 0, failed = 0;
		Class testClass = Class
				.forName("bill.study.annotation.effectivejava.item35.Sample2");
		for (Method m : testClass.getDeclaredMethods()) {
			if (m.isAnnotationPresent(ExceptionTest.class)) {
				try {
					m.invoke(null);
					System.out.printf("Test %s failed: no exception%n", m);
				} catch (InvocationTargetException ex) {
					Throwable exc = ex.getCause();
					Class<? extends Exception>[] excTypes = m.getAnnotation(
							ExceptionTest.class).value();
					int oldPassed = passed;
					for (Class<? extends Exception> excType : excTypes) {
						if (excType.isInstance(exc)) {
							passed++;
							break;
						} 
					}
					if(passed == oldPassed) {
						System.out.printf("Test %s failed: %s %n", m, ex
								.getCause());
						failed++;
					}
				} catch (Exception exc) {
					System.out.println("INVALID @Test: " + m);
				}
			}
		}
		System.out.printf("Passed: %d, Failed %d%n", passed, failed);
	}
}